import { ref } from 'vue'
import { getOrderList as getOrderListApi } from '@/api/order'

/**
 * 工单列表服务
 */
export default function () {
  const getOrderListLoading = ref(false)
  const orderList = ref<Array<OrderAPIResponse.OrderListItem>>([])
  const orderListTotal = ref(0)
  const getOrderListParam = ref<OrderRequestParam.OrderList>({
    addresses: [],
    area: '',
    areas: [],
    childStatus: '',
    city: '',
    citys: [],
    dateFieldType: '',
    dateType: '',
    empIds: [],
    empType: '',
    factoryIds: [],
    fieldId: '',
    fieldSearch: '',
    goodsSearch: '',
    isHs: '',
    isHx: '',
    isPause: '',
    limit: 50,
    maxServiceTime: '',
    minServiceTime: '',
    moduleIds: [],
    networkIds: [],
    networkType: '',
    orderEndTime: '',
    orderNo: '',
    orderNodeTimeoutType: '',
    orderStartTime: '',
    orderUserSearch: '',
    page: 1,
    province: '',
    provinces: [],
    serviceTimeType: '',
    sources: [],
    statues: [],
    status: '',
    workLevelIds: [],
  })

  const resetGetOrderListParam = () => {
    getOrderListParam.value = {
      addresses: [],
      area: '',
      areas: [],
      childStatus: '',
      city: '',
      citys: [],
      dateFieldType: '',
      dateType: '',
      empIds: [],
      empType: '',
      factoryIds: [],
      fieldId: '',
      fieldSearch: '',
      goodsSearch: '',
      isHs: '',
      isHx: '',
      isPause: '',
      limit: 50,
      maxServiceTime: '',
      minServiceTime: '',
      moduleIds: [],
      networkIds: [],
      networkType: '',
      orderEndTime: '',
      orderNo: '',
      orderNodeTimeoutType: '',
      orderStartTime: '',
      orderUserSearch: '',
      page: 1,
      province: '',
      provinces: [],
      serviceTimeType: '',
      sources: [],
      statues: [],
      status: '',
      workLevelIds: [],
    }
  }

  const getOrderList = async () => {
    try {
      getOrderListLoading.value = true
      const res = await getOrderListApi(getOrderListParam.value)
      getOrderListLoading.value = false
      if (res.data.code === 20000) {
        orderListTotal.value = Number(res.data.data.total)
        orderList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (error) {
      getOrderListLoading.value = false
      return '获取工单列表失败，请稍后重试'
    }
  }

  return {
    orderList,
    orderListTotal,
    getOrderListParam,
    getOrderListLoading,
    getOrderList,
    resetGetOrderListParam,
  }
}
